package pack01_zookeeper;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.junit.Test;

/**
 * @author lilulu
 * @date 2023/2/9 14:00
 */
public class TestDemo01 {

    /*
 创建节点
 */
    @Test
    public void createZnode() throws Exception {
        //1:定制一个重试策略
	/*
		param1: 重试的间隔时间
		param2:重试的最大次数
	 */
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,1);
        //2:获取一个客户端对象
	/*
	   param1:要连接的Zookeeper服务器列表
	   param2:会话的超时时间
	   param3:链接超时时间
	   param4:重试策略
	 */
        String connectionStr = "192.168.88.161:2181,192.168.88.162:2181,192.168.88.163:2181";
        CuratorFramework client = CuratorFrameworkFactory.newClient(connectionStr, retryPolicy);

        //3:开启客户端
        client.start();
        //4:创建节点
	/*
	  节点类型:
		   CreateMode.PERSISTENT:永久节点
		   CreateMode.PERSISTENT_SEQUENTIAL:永久序列化节点
		   CreateMode.EPHEMERAL:临时节点
		   CreateMode.EPHEMERAL_SEQUENTIAL:临时序列化节点
	   /hello2 :节点路径
	   world :节点数据
	 */
        client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/hello2","world".getBytes());
        //5:关闭客户端
        client.close();
    }

}
